package com.skeeey.aim.service.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.skeeey.aim.model.Dealer;
import com.skeeey.aim.service.dao.DealerDao;

public class DealerDaoImpl extends TempleteDaoImpl<Dealer, String> implements DealerDao {

	@Override
	public void saveDealers(List<Dealer> dealers) {
		Session session = this.getSession();
		for (int i = 0; i < dealers.size(); i++) {
			session.save(dealers.get(i));
			if (i % 50 == 0) {
				session.flush();
				session.clear();
			}
		}
		
		session.flush();
		session.clear();
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Dealer> findDealersBy(String dealerCode) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Dealer.class);
		criteria.add(Restrictions.eq("netCode", dealerCode));
		List<Dealer> dealers = this.getHibernateTemplate().findByCriteria(criteria);
		return dealers;
	}

}
